<?php
/**
 * Project:     EQdkp - Open Source Points System
 * License:     http://eqdkp.com/?p=license
 * -----------------------------------------------------------------------
 * File:        logs.php
 * Began:       Tues December 24 2002
 * Date:        $Date: 2008-03-08 15:29:17 +0000 (Sat, 08 Mar 2008) $
 * -----------------------------------------------------------------------
 * @author      $Author: rspeicher $
 * @copyright   2002-2008 The EQdkp Project Team
 * @link        http://eqdkp.com/
 * @package     eqdkp
 * @version     $Rev: 516 $
 */

define('EQDKP_INC', true);
define('IN_ADMIN', true);
$eqdkp_root_path = './../';
require_once($eqdkp_root_path . 'common.php');

$sort_order = array(
    0 => array('log_date desc', 'log_date'),
    1 => array('log_type', 'log_type desc'),
    2 => array('user_name', 'user_name desc'),
    3 => array('log_ipaddress', 'log_ipaddress desc'),
    4 => array('log_result', 'log_result desc')
);

$current_order = switch_order($sort_order);

// Obtain var settings
$log_id = $in->get(URI_LOG, 0);
$action = ( $log_id ) ? 'view' : 'list';

$user->check_auth('a_logs_view');

//
// Processing
//
$valid_action_types = array(
    '{L_ACTION_EVENT_ADDED}'      => $user->lang['action_event_added'],
    '{L_ACTION_EVENT_UPDATED}'    => $user->lang['action_event_updated'],
    '{L_ACTION_EVENT_DELETED}'    => $user->lang['action_event_deleted'],
    '{L_ACTION_GROUPADJ_ADDED}'   => $user->lang['action_groupadj_added'],
    '{L_ACTION_GROUPADJ_UPDATED}' => $user->lang['action_groupadj_updated'],
    '{L_ACTION_GROUPADJ_DELETED}' => $user->lang['action_groupadj_deleted'],
    '{L_ACTION_INDIVADJ_ADDED}'   => $user->lang['action_indivadj_added'],
    '{L_ACTION_INDIVADJ_UPDATED}' => $user->lang['action_indivadj_updated'],
    '{L_ACTION_INDIVADJ_DELETED}' => $user->lang['action_indivadj_deleted'],
    '{L_ACTION_ITEM_ADDED}'       => $user->lang['action_item_added'],
    '{L_ACTION_ITEM_UPDATED}'     => $user->lang['action_item_updated'],
    '{L_ACTION_ITEM_DELETED}'     => $user->lang['action_item_deleted'],
    '{L_ACTION_MEMBER_ADDED}'     => $user->lang['action_member_added'],
    '{L_ACTION_MEMBER_UPDATED}'   => $user->lang['action_member_updated'],
    '{L_ACTION_MEMBER_DELETED}'   => $user->lang['action_member_deleted'],
    '{L_ACTION_NEWS_ADDED}'       => $user->lang['action_news_added'],
    '{L_ACTION_NEWS_UPDATED}'     => $user->lang['action_news_updated'],
    '{L_ACTION_NEWS_DELETED}'     => $user->lang['action_news_deleted'],
    '{L_ACTION_RAID_ADDED}'       => $user->lang['action_raid_added'],
    '{L_ACTION_RAID_UPDATED}'     => $user->lang['action_raid_updated'],
    '{L_ACTION_RAID_DELETED}'     => $user->lang['action_raid_deleted'],
    '{L_ACTION_TURNIN_ADDED}'     => $user->lang['action_turnin_added']);
    
$valid_action_types = array_merge($valid_action_types, $pm->get_log_actions());

switch ( $action )
{
    case 'view':
        // Get log info
        $sql = "SELECT l.*, u.user_name 
                FROM __logs AS l LEFT JOIN __users AS u ON u.`user_id` = l.`admin_id`
                WHERE (`log_id` = '{$log_id}')";
        $result = $db->query($sql);
        $log = $db->fetch_record($result);
        $db->free_result($result);
        
        eval($log['log_action']);

        if ( !empty($log_action['header']) )
        {
            $log_header = lang_replace($log_action['header']);
        }

        $eqdkp->switch_row_class();

        foreach ( $log_action as $k => $v )
        {
            if ( $k != 'header' )
            {
                $k = lang_replace($k);
                $v = lang_replace($v);

                $tpl->assign_block_vars('log_row', array(
                    'ROW_CLASS' => $eqdkp->switch_row_class(),
                    'KEY'       => sanitize($k) . ':',
                    'VALUE'     => nl2br(sanitize($v))
                ));
            }
        }

        $tpl->assign_vars(array(
            'S_LIST' => false,

            'L_LOG_VIEWER' => $user->lang['viewlogs_title'],
            'L_DATE'       => $user->lang['date'],
            'L_USERNAME'   => $user->lang['username'],
            'L_IP_ADDRESS' => $user->lang['ip_address'],
            'L_SESSION_ID' => $user->lang['session_id'],

            'LOG_DATE'       => ( !empty($log['log_date']) ) ? date($user->style['date_time'], $log['log_date']) : '&nbsp;',
            'LOG_USERNAME'   => ( !empty($log['user_name']) ) ? sanitize($log['user_name']) : '&nbsp;',
            'LOG_IP_ADDRESS' => sanitize($log['log_ipaddress']),
            'LOG_SESSION_ID' => sanitize($log['log_sid']),
            'LOG_ACTION'     => ( !empty($log_header) ) ? sanitize($log_header) : '&nbsp;')
        );

        break;
    case 'list':
        $sql = "SELECT l.*, u.user_name 
                FROM __logs AS l LEFT JOIN __users AS u ON u.`user_id` = l.`admin_id`";
        $addon_sql = '';
        $search_term = '';

        // If they're looking for something specific, we have to figure out 
        // what that is
        $search_term = $in->get('search');
        if ( $search_term != '' )
        {
            // Check if it's an action
            if ( in_array($search_term, $valid_action_types) )
            {
                foreach ( $valid_action_types as $k => $v )
                {
                    if ( $v == $search_term )
                    {
                        $addon_sql = " WHERE (l.`log_type` = '" . $db->escape($k) . "')";
                    }
                }
            }
            // Check if it's an IP
            elseif ( preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $search_term) )
            {
                $addon_sql = " WHERE (l.`log_ipaddress` = '" . $db->escape($search_term) . "')";
            }
            // Still going? It's a username
            else
            {
                $addon_sql = " WHERE (u.`user_name` = '" . $db->escape($search_term) . "')";
            }
        }

        $total_sql = "SELECT COUNT(*)
                      FROM __logs AS l LEFT JOIN __users AS u ON u.`user_id` = l.`admin_id`";
        $total_logs = $db->query_first($total_sql . $addon_sql);

        $start = $in->get('start', 0);

        $result = $db->query("{$sql} {$addon_sql} ORDER BY {$current_order['sql']} LIMIT {$start},100");
        while ( $log = $db->fetch_record($result) )
        {
            $log['log_type']   = lang_replace($log['log_type']);
            $log['log_result'] = lang_replace($log['log_result']);

            $tpl->assign_block_vars('logs_row', array(
                'ROW_CLASS'    => $eqdkp->switch_row_class(),
                'DATE'         => ( !empty($log['log_date']) ) ? date($user->style['date_time'], $log['log_date']) : '&nbsp;',
                'TYPE'         => ( !empty($log['log_type']) ) ? $log['log_type'] : '&nbsp;',
                'U_VIEW_LOG'   => log_path($log['log_id']),
                'USER'         => sanitize($log['user_name']),
                'IP'           => sanitize($log['log_ipaddress']),
                'RESULT'       => sanitize($log['log_result']),
                'C_RESULT'     => ( $log['log_result'] == $user->lang['success'] ) ? 'positive' : 'negative',
                'ENCODED_TYPE' => urlencode(sanitize($log['log_type'])),
                'ENCODED_USER' => urlencode(sanitize($log['user_name'])),
                'ENCODED_IP'   => urlencode(sanitize($log['log_ipaddress']))
            ));
        }

        $tpl->assign_vars(array(
            'S_LIST' => true,

            'L_DATE'        => $user->lang['date'],
            'L_TYPE'        => $user->lang['type'],
            'L_VIEW_ACTION' => $user->lang['view_action'],
            'L_USER'        => $user->lang['user'],
            'L_IP_ADDRESS'  => $user->lang['ip_address'],
            'L_RESULT'      => $user->lang['result'],
            'L_VIEW'        => $user->lang['view'],

            'O_DATE'        => $current_order['uri'][0],
            'O_TYPE'        => $current_order['uri'][1],
            'O_USER'        => $current_order['uri'][2],
            'O_IP'          => $current_order['uri'][3],
            'O_RESULT'      => $current_order['uri'][4],

            'U_LOGS'        => path_default('admin/logs.php') . path_params(array('search' => $search_term, 'start' => $start)) .'&amp;',
            'U_LOGS_SEARCH' => path_default('admin/logs.php') . '&amp;',

            'CURRENT_ORDER'       => $current_order['uri']['current'],
            'START'               => $start,
            'VIEWLOGS_FOOTCOUNT'  => sprintf($user->lang['viewlogs_footcount'], $total_logs, 100),
            'VIEWLOGS_PAGINATION' => generate_pagination(path_default('admin/logs.php') . path_params(array('search' => $search_term, URI_ORDER, $current_order['uri']['current'])),
                                     $total_logs, '100', $start)
        ));
        break;
}

$eqdkp->set_vars(array(
    'page_title'    => page_title($user->lang['viewlogs_title']),
    'template_file' => 'admin/logs.html',
    'display'       => true
));

/**
* Returns the language value of a variable in the format "{L_<LANG_KEY>}"
* Allows use of template-style variables outside of templates
*
* @param $variable Variable to replace language keys for
* @return string Translated variable
*/
function lang_replace($variable)
{
    global $user;

    preg_match("/\{L_(.+)\}/", $variable, $to_replace);
    if ( (isset($to_replace[1])) && (isset($user->lang[strtolower($to_replace[1])])) )
    {
        $variable = str_replace('{L_'.$to_replace[1].'}', $user->lang[strtolower($to_replace[1])], $variable);
    }

    return $variable;
}